// dlgapproxgpolygon.cpp : implementation file
//

#include "stdafx.h"
#include "dlgapproxgpolygon.h"
#include "operatoredit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlagPproxgPolygon dialog
int CDlagPproxgPolygon::pos=50;
double CDlagPproxgPolygon::down=0;
double CDlagPproxgPolygon::up=10;

CDlagPproxgPolygon::CDlagPproxgPolygon(CWnd* pParent /*=NULL*/,COpApproxCur* lpopcheck)
	: CBCGPDialog(CDlagPproxgPolygon::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlagPproxgPolygon)
	m_tol_down = CDlagPproxgPolygon::down;
	m_tol_up = CDlagPproxgPolygon::up;
	m_pos=CDlagPproxgPolygon::pos;
	
	m_lpoperatorcheck=lpopcheck;
	
	m_numorgpts=0.0f;
	m_numnowpts=0.0f;
	m_precsion=0.0f;
	//}}AFX_DATA_INIT
}


void CDlagPproxgPolygon::DoDataExchange(CDataExchange* pDX)
{
	CBCGPDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlagPproxgPolygon)
	DDX_Control(pDX, IDC_SLIDER_PRECISION, m_slide);
	DDV_MinMaxDouble(pDX, m_tol_down, 0., 1000000.);
	DDX_Text(pDX, IDC_UTILITY_UPLIMIT, m_tol_up);
	DDV_MinMaxDouble(pDX, m_tol_up, 1.e-006, 1000000.);

	DDX_Text(pDX, IDC_UTILITY_SHOW_PREC, m_precsion);
	DDX_Text(pDX, IDC_SHOW_ORG_PTSNUM, m_numorgpts);
	DDX_Text(pDX, IDC_SHOW_NOW_PTSNUM, m_numnowpts);
	//}}AFX_DATA_MAP
}

void CDlagPproxgPolygon::OnNcDestroy() 
{
	//StoreValue();
	CBCGPDialog::OnNcDestroy();
	delete this;	
	// TODO: Add your message handler code here
	
}


BEGIN_MESSAGE_MAP(CDlagPproxgPolygon, CBCGPDialog)
	//{{AFX_MSG_MAP(CDlagPproxgPolygon)
	ON_WM_NCDESTROY()
//	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_PRECISION, OnReleasedcaptureSliderPrecision)
//	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_PRECISION, OnCustomdrawSliderPrecision)
//	ON_NOTIFY(NM_OUTOFMEMORY, IDC_SLIDER_PRECISION, OnOutofmemorySliderPrecision)
	ON_EN_CHANGE(IDC_UTILITY_UPLIMIT, OnChangeInvalidateValue)//
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlagPproxgPolygon message handlers

BOOL CDlagPproxgPolygon::OnInitDialog() 
{
	CBCGPDialog::OnInitDialog();

	EnableVisualManagerStyle(TRUE,TRUE);

	m_slide.SetRange(0,100);
	m_slide.SetLineSize(1);
	m_slide.SetPageSize(5);
	m_slide.SetPos(m_pos);
	m_precsion=this->GetSlideValue();	

	UpdateData(FALSE);
	// TODO: Add extra initialization here	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

/*
void CDlagPproxgPolygon::OnReleasedcaptureSliderPrecision(NMHDR* pNMHDR, LRESULT* pResult) 
{
    OnChangeInvalidateValue();
	
	*pResult = 0;
}*/


double CDlagPproxgPolygon::GetSlideValue()
{
	ASSERT(m_tol_up>=m_tol_down);
	int up,down;
	m_slide.GetRange(down,up);
	return fabs(m_tol_up-m_tol_down)*m_slide.GetPos()/abs(up-down);
}

/*
void CDlagPproxgPolygon::OnCustomdrawSliderPrecision(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

void CDlagPproxgPolygon::OnOutofmemorySliderPrecision(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}
*/

void CDlagPproxgPolygon::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	OnChangeInvalidateValue();
	CBCGPDialog::OnHScroll(nSBCode, nPos, pScrollBar); //DEL BY TZH 
}



void CDlagPproxgPolygon::OnChangeInvalidateValue() 
{
     InvalidateInformation(TRUE); //process 
	 InvalidateInformation(FALSE);//get points be calculated number 
}

void CDlagPproxgPolygon::InvalidateInformation(BOOL bInavlidate)
{
	if(!m_lpoperatorcheck) return;
	if(!bInavlidate)
	{
		m_numorgpts=m_lpoperatorcheck->m_num_orgpts;
		m_numnowpts=m_lpoperatorcheck->m_num_nowpts;
		m_precsion=this->GetSlideValue();

		UpdateData(FALSE);
	}
	else
	{

		UpdateData(TRUE);

		CDlagPproxgPolygon::pos=m_slide.GetPos();
		CDlagPproxgPolygon::down=this->m_tol_down;
		CDlagPproxgPolygon::up=this->m_tol_up;	

		m_lpoperatorcheck->DM_SetTolerance(this->GetSlideValue()); //Get INFO and PRocess
		
		m_lpoperatorcheck->DM_EraseTemporaryGraph(TRUE); //remove temporary graph
		m_lpoperatorcheck->DM_Flush();                   //remove the memory 
		
		if(m_lpoperatorcheck->DM_CollectPolygonPoints())			
		m_lpoperatorcheck->DM_EraseTemporaryGraph(FALSE);
	}
	
}
